class GameLoginScene extends eui.Component {

    private accountLoginBtn: eui.Button;
    private vxLoginBtn: eui.Button;
    private loginBtn: eui.Button;
    private registerBtn: eui.Button;
    private backBtn: eui.Button;
    private registerAccountBtn: eui.Button;

    private passwordText: eui.GTextInput;
    private passwordRepeatText: eui.GTextInput;
    private usernameText: eui.GTextInput;


    private rmbPass: eui.CheckBox;

    public static LoginViewState: string = "loginView";
    public static AccountLoginState: string = "accountLogin";
    public static RegisterState: string = "registerView";

    public constructor() {
        super();
        this.skinName = "resource/gameSkins/scene/GameLoginSceneSkin.exml";
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAdd, this);
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemove, this);

    }
    public createChildren() {
        super.createChildren();
    }

    public setData(state: string): void {
        this.setCurrentState(state);
    }

    public setCurrentState(v: string) {
        var self = this;
        self.currentState = v;
        if (v == GameLoginScene.AccountLoginState) {
            var account = App.getItem("account");
            var password = App.getItem("password");
            if (account) self.usernameText.text = account;
            if (password) self.passwordText.text = password;
            if (App.getItem("rmbPass") == "true") self.rmbPass.selected = true;
            else self.rmbPass.selected = false;
        }
        self.validateNow();  //这个在整个场景都修改之后在执行 一般放在最后
    }
    private onAdd(e: egret.Event) {
        this.addEventListener(egret.TouchEvent.TOUCH_END, this.onTouch, this);

    }
    private onRemove(e: egret.Event) {
        this.removeEventListener(egret.TouchEvent.TOUCH_END, this.onTouch, this);

    }

    private onTouch(e: egret.TouchEvent) {
        var self = this;
        switch (e.target) {
            case self.accountLoginBtn:
                self.setCurrentState(GameLoginScene.AccountLoginState);
                break;
            case self.vxLoginBtn:
                //UIManager.instance.showTip("微信暂时无法登陆.");
                EffectUtilsModule.showTips("微信暂时无法登陆.", Math.ceil(Math.random() * 5), true);
                //EffectUtilsModule.playEffect(self.vxLoginBtn,Math.ceil(Math.random()*3)) ;
                //EffectUtilsModule.rockObj(self.accountLoginBtn,1000);
                //EffectUtilsModule.flyObj(self.accountLoginBtn,1000);
                //EffectUtilsModule.shakeScreen(App.root,2);
                EffectUtilsModule.shakeObj(this);
                //EffectUtilsModule.rotationEffect(this.accountLoginBtn);
                EffectUtilsModule.playScaleEffect(self.accountLoginBtn);
                break;
            case self.registerAccountBtn:
                self.setCurrentState(GameLoginScene.RegisterState);
                break;
            case self.loginBtn:
                self.onAccountLogin();
                break;
            case self.registerBtn:
                self.onRegister();
                break;
            case self.backBtn:
                self.backToPrev();
                break;



        }
    }

    private onAccountLogin() {
        var self = this;
        App.saveItem("account", self.usernameText.text);
        App.saveItem("password", self.passwordText.text);
        if (self.rmbPass.selected) App.saveItem("rmbPass", "true");
        else App.saveItem("rmbPass", "false");
        var paramVariable = new egret.URLVariables("username=" + self.usernameText.text + "&password=" + self.passwordText.text);
        App.Http.send(HttpMessageTypeName.LOGIN, paramVariable, HttpRouteConst.LOGIN_ROUTE);

    }

    private onRegister() {
        var self = this;
        var username = self.usernameText.text;
        var password = self.passwordText.text;
        var repeatPasswored = self.passwordRepeatText.text;
        if (username.length == 0) {
            UIManager.instance.showTip("账号不能为空.");
        } else if (password.length == 0) {
            UIManager.instance.showTip("密码不能为空.");
        } else if (password == repeatPasswored) {
            UIManager.instance.showTip("密码输入不一致，请重新输入.");
        } else {
            var paramVariable = new egret.URLVariables("username=" + username + "&password=" + password);
            App.Http.send(HttpMessageTypeName.REGISTER, paramVariable, HttpRouteConst.REGISTER_ROUTE);
        }
    }

    private backToPrev() {
        var self = this;
        self.setCurrentState(GameLoginScene.LoginViewState);
    }

    private resetInputData() {
        var self = this;
        self.usernameText.text = "";
        self.passwordText.text = "";
        self.passwordRepeatText.text = "";
    }
}